package com.company.zhishidian.socket;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {
        try (Socket client = new Socket("localhost", 8888)) {
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            Scanner input = new Scanner(System.in);
            Scanner in = new Scanner(client.getInputStream());//向自己输入

            //耳朵
            new Thread(()->{
                while(true) {
                    String msg = in.nextLine();//IO阻塞
                    System.out.println(msg);
                }
            }).start();

            //嘴巴
            while(true) {
                  out.println(input.nextLine());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
